package com.ruoyi.outbound.consignment.service.impl;

import java.util.Date;
import java.util.List;

import com.ruoyi.basicData.product.domain.Product;
import com.ruoyi.basicData.product.mapper.ProductMapper;
import com.ruoyi.basicData.ruleSetting.service.RuleSettingService;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.outbound.outbound.mapper.OutboundMapper;
import com.ruoyi.storage.tickets.domain.Tickets;
import com.ruoyi.sync.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.outbound.consignment.mapper.ConsignmentMapper;
import com.ruoyi.outbound.consignment.domain.Consignment;
import com.ruoyi.outbound.consignment.service.IConsignmentService;

/**
 * 发货通知单Service业务层处理
 * 
 * @author zzx
 * @date 2024-02-07
 */
@Service
public class ConsignmentServiceImpl extends BaseService implements IConsignmentService
{
    @Autowired
    private ConsignmentMapper consignmentMapper;
    @Autowired
    private RuleSettingService ruleSettingService;
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private OutboundMapper outboundMapper;
    /**
     * 查询发货通知单
     * 
     * @param consignmentId 发货通知单主键
     * @return 发货通知单
     */
    @Override
    public Consignment selectConsignmentByConsignmentId(String consignmentId)
    {
        return consignmentMapper.selectConsignmentByConsignmentId(consignmentId);
    }

    /**
     * 查询发货通知单列表
     * 
     * @param consignment 发货通知单
     * @return 发货通知单
     */
    @Override
    public List<Consignment> selectConsignmentList(Consignment consignment)
    {
        return consignmentMapper.selectConsignmentList(consignment);
    }

    /**
     * 新增发货通知单
     * 
     * @param consignment 发货通知单
     * @return 结果
     */
    @Override
    public int insertConsignment(Consignment consignment)
    {
        String consignment_id = ruleSettingService.genDocId(Consignment.class, "CONSIGNMENT_ID");
        consignment.setConsignmentId(consignment_id);
//        Product product = productMapper.selectProductByProductId(consignment.getProductId());
//        consignment.setProductName(product.getProductName());
        consignment.setDate(new Date());
        consignment.setQuantityShipped(0L);
        consignment.setReturnNumber(0L);
        consignment.setDelFlag(2L);
        consignment.setAudit(0L);
        consignment.setClose(0L);
        return consignmentMapper.insertConsignment(consignment);
    }

    /**
     * 修改发货通知单
     * 
     * @param consignment 发货通知单
     * @return 结果
     */
    @Override
    public int updateConsignment(Consignment consignment)
    {
        return consignmentMapper.updateConsignment(consignment);
    }

    /**
     * 批量删除发货通知单
     * 
     * @param consignmentIds 需要删除的发货通知单主键
     * @return 结果
     */
    @Override
    public int deleteConsignmentByConsignmentIds(String[] consignmentIds)
    {
        return consignmentMapper.deleteConsignmentByConsignmentIds(consignmentIds);
    }

    /**
     * 删除发货通知单信息
     * 
     * @param consignmentId 发货通知单主键
     * @return 结果
     */
    @Override
    public int deleteConsignmentByConsignmentId(String consignmentId)
    {
        return consignmentMapper.deleteConsignmentByConsignmentId(consignmentId);
    }

    /**
     * 审核/反审核出库单
     * @param consignmentId
     * @return
     */
    @Override
    public AjaxResult updateAudit(String consignmentId) {
        Consignment consignment = consignmentMapper.selectConsignmentByConsignmentId(consignmentId);
        if (consignment.getAudit()==0){
            consignmentMapper.updateOnAudit(consignmentId);
            return AjaxResult.success();
        }else {
            if (outboundMapper.selectNumberByConsignmentId(consignmentId)==0){
                consignmentMapper.updateUnAudit(consignmentId);
                return AjaxResult.success();
            }else {
                return AjaxResult.error(500,"该发货通知单，已有后续操作，无法反审核");
            }

        }

    }
    /**
     * 结案/反结案出库单
     * @param consignmentId
     * @return
     */
    @Override
    public AjaxResult updateClose(String consignmentId) {
        Consignment consignment = consignmentMapper.selectConsignmentByConsignmentId(consignmentId);
        if (consignment.getClose()==0){
            consignmentMapper.updateOnClose(consignmentId);
        }else {
            consignmentMapper.updateUnClose(consignmentId);
        }
        return AjaxResult.success();
    }

}
